package com.saile.service;

import com.saile.dao.MediaMapper;
import com.saile.domain.Media;
import com.saile.myRunTime.MyRunTimeExcption;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Baggio on 2018-4-24.
 */
@Service
public class MediaServiceImpl extends BaseService implements MediaService {
    @Autowired
    MediaMapper mediaMapper;


    private void insertMedia(Media media) {
        mediaMapper.insertMedia(media);
    }

    private void updateMedia(Media media) {
        media.setCreateTime(new Date());
        mediaMapper.updateMediaByPrimaryKeySelective(media);
    }

    @Override
    public void modifyMedia(Media media) throws MyRunTimeExcption {
        if (media.getTid() == null) {
            insertMedia(media);
            return;
        }
        updateMedia(media);
    }

    @Override
    public List<Media> getMediaList(Map<String, String> map) throws MyRunTimeExcption {
        return mediaMapper.getMediaList(map);
    }

    @Override
    public void deleteMedia(Map<String,String> map) throws MyRunTimeExcption {
        mediaMapper.deleteMedia(map);
    }

    @Override
    public Media getMedia(Map<String, String> map) throws MyRunTimeExcption {
        return mediaMapper.getMedia(map);
    }

    @Override
    public List<Media> getGuessYouLike(Map<String, String> map) throws MyRunTimeExcption {

        String length = map.get("length");
        if (StringUtils.isBlank(length)) {
            length = "10";
        }
        return mediaMapper.getGuessYouLike(map);
    }

    @Override
    public List<Media> recommendVideo() throws MyRunTimeExcption {
        return mediaMapper.recommendVideo();
    }
}
